/*
 * $Id: ApplicationInfo.java,v 1.10 2012/01/26 16:23:11 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import java.util.Date;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IPropertiesDAO;
import com.ge.healthcare.autosc.common.exceptions.PropertyException;

/**
 * Purpose: ApplicationInfo provides access to application properties. The
 * ApplicationInfo has scheduler to reload application properties according to
 * configured refresh rate insides the application properties file. The default
 * refresh rate is once per day.
 * @author Anthony Siu, Navdeep Mahajan
 */
public class ApplicationInfo implements ApplicationContextAware {
	private Properties properties = null;

	private static ApplicationInfo applicationInfo = null;
	private static ApplicationContext applicationContext = null;

	private static String stepTypeName = null;
	private static IPropertiesDAO propertiesDAO = null;

	/**
	 * Private constructor. It loads properties and start the reload timer
	 * 
	 * @throws PropertyException
	 */
	private ApplicationInfo(String stepTypeName, IPropertiesDAO propertiesDAO) {
		ApplicationInfo.stepTypeName = stepTypeName;
		ApplicationInfo.propertiesDAO = propertiesDAO;
		loadProperties();
		applicationInfo = this;
	}

	private ApplicationInfo() {
		this(stepTypeName, propertiesDAO);
	}

	/**
	 * Singleton interface to retrieve ApplicationInfo instance
	 * 
	 * @return ApplicationInfo instance
	 * @throws PropertyException
	 */
	public static ApplicationInfo getInstance() {
		if (applicationInfo == null) {
			applicationInfo = new ApplicationInfo();
		}
		return applicationInfo;
	}
	
	/**
	 * Returns default value in value for the property is not available
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public String getProperty(String propertyName, String defaultValue) {
		String value = this.getProperty(propertyName);
		if ( ASCUtil.isNullOrEmpty(value)){
			value = defaultValue;
		}
		return value;
	}

	/**
	 * Get application property by property name
	 * 
	 * @param propertyName
	 * @return property value or null if property is not defined
	 * @throws PropertyException
	 */
	public String getProperty(String propertyName) {
		String value = null;
		synchronized (ApplicationInfo.class) {
			if (ASCUtil.isNullOrEmptyObject(properties)) {
				loadProperties();
			}
		}
		if (!ASCUtil.isNullOrEmptyObject(properties)) {
			value = properties.getProperty(propertyName);
		}
		return value;
	}
	
	/**
	 * Returns the Boolean property value
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public boolean getBooleanProperty(String propertyName, String defaultValue){
		String value = this.getProperty(propertyName, defaultValue);
		return Boolean.valueOf(value);
	}

	/**
	 * Converts property value to long and then returns the same
	 * 
	 * @param propertyName
	 * @return
	 */
	public long getLongPropertyValue(String propertyName) {
		return Long.parseLong(this.getProperty(propertyName));
	}

	/**
	 * Converts property value to long and then returns the same
	 * 
	 * @param propertyName
	 * @return
	 */
	public long getLongPropertyValue(String propertyName, String defaultValue) {
		return Long.parseLong(this.getProperty(propertyName, defaultValue));
	}
	
	/**
	 * Get required application property by property name
	 * 
	 * @param propertyName
	 * @return property value
	 * @throws PropertyException
	 *             - if property is not defined
	 */
	public String getRequiredPropertyValue(String propertyName)
			throws PropertyException {
		String value = null;
		synchronized (ApplicationInfo.class) {
			if (ASCUtil.isNullOrEmptyObject(properties)) {
				loadProperties();
			}
		}
		if (!ASCUtil.isNullOrEmptyObject(properties)) {
			value = properties.getProperty(propertyName);
		}
		if (ASCUtil.isNullOrEmpty(value)) {
			throw new PropertyException(new StringBuffer().append(
					"Value for property : ").append(propertyName).append(
					" is null or empty").toString());
		}
		return value;
	}
	
	/**
	 * Returns integer value for a property
	 * @param propertyName
	 * @param defaultValue
	 * @return
	 */
	public int getIntegerPropertyValue(String propertyName, String defaultValue){
		String value = this.getProperty(propertyName, defaultValue).trim();
		return Integer.parseInt(value);
	}

	/**
	 * Get all application properties
	 * 
	 * @return application properties
	 * @throws PropertyException
	 */
	public Properties getProperties() {
		synchronized (ApplicationInfo.class) {
			if (properties != null) {
				return properties;
			}
			loadProperties();
		}
		return properties;
	}

	/**
	 * Set Properties object
	 * 
	 * @param properties
	 */
	public void setProperties(Properties properties) {
		synchronized (ApplicationInfo.class) {
			this.properties = properties;
		}
	}

	/*
	 * Load properties from autosc properties file
	 */
	public void loadProperties() {
		synchronized (ApplicationInfo.class) {
			ASCLogger.info(ApplicationInfo.class, "loadProperties", "Invoked at:" + new Date());
			properties = propertiesDAO.getForStepType(stepTypeName);
		}
	}

	/**
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		ApplicationInfo.applicationContext = applicationContext;
	}

	/**
	 * @return the applicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
}
